package org.example.myleet.p500;

public class Solution {
    /**
     * 暴力模拟，哈希加速
     */
    public String[] findWords(String[] words) {
        //将键盘映射成3行，分别记录每行的字符在ASCII中字符是否存在
        char[] line1 = "qwertyuiop".toCharArray();
        char[] line2 = "asdfghjkl".toCharArray();
        char[] line3 = "zxcvbnm".toCharArray();
        boolean[] l1 = new boolean[128];
        boolean[] l2 = new boolean[128];
        boolean[] l3 = new boolean[128];
        for (char c : line1) {
            l1[c] = true;
        }
        for (char c : line2) {
            l2[c] = true;
        }
        for (char c : line3) {
            l3[c] = true;
        }
        String[] result = new String[words.length];
        int size = 0;
        tag:
        for (String word : words) {
            //针对每个单词，由首字母的小写形式判断键盘行
            char[] charArr = word.toCharArray();
            boolean[] l = null;
            if (l1[Character.toLowerCase(charArr[0])]) {
                l = l1;
            }
            if (null == l) {
                if (l2[Character.toLowerCase(charArr[0])]) {
                    l = l2;
                } else {
                    l = l3;
                }
            }
            for (char c : word.toCharArray()) {
                if (!l[Character.toLowerCase(c)]) {
                    //出现一个字符不在同一行的直接结束，到下一个单词
                    continue tag;
                }
            }
            result[size++] = word;
        }
        String[] ret = new String[size];
        System.arraycopy(result, 0, ret, 0, size);
        return ret;
    }
}
